// $Id: editview.js,v 1.1 2009/02/17 00:39:02 agileware Exp $

Drupal.behaviors.editviewLocation = function (context) {
    $('form', context).each(EditviewLocation.formCapture);
};

var EditviewLocation = EditviewLocation || {
    formCapture: function() {
	var form_id = $('[@name=form_id]', this).val();
	var delete_form = (form_id == 'editview_location_delete_form');
	var location_form = form_id && form_id.match(/^editview_location_form_\d+/);
	if (location_form || delete_form) {
	    var options = {
		url: Drupal.settings.editviewLocation.submit_url,
		beforeSubmit: function(form_values, form, options) {
		    var name;
		    var deleted = false;
		    for (var i = 0; i < form_values.length; i++) {
			if (form_values[i].name == 'op' && form_values[i].value == Drupal.t('Delete')) {
			    deleted = true;
			}
			if (form_values[i].name == 'title') {
			    name = form_values[i].value;
			}
		    }
		    if (deleted && !confirm(Drupal.t('Are you sure you want to delete @name?', {
			'@name': name
		    }) +"\n"+ Drupal.t('This action cannot be undone.'))) {
			return false;
		    }

		    $(form)
			.parent()
			.fadeTo('slow', 0.1);
		},
		success: function(response, status, form) {
		    Drupal.attachBehaviors(
			$(form)
			    .parent()
				//remove all previous status messages
				.parents('.view-content')
				    .find('.messages.status')
				    .remove()
				    .end()
				.end()
			    .empty()
			    .append(response)
			    .fadeTo('fast', 1)
		    );
		},
		dataType: 'json',
		type: 'POST'
	    }
	    $(this).ajaxForm(options);
	}
    }
};
